package com.taskflow.init;

import com.taskflow.entity.DownloadTask;
import com.taskflow.service.DownloadTaskService;
import com.taskflow.service.TaskSchedulerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 应用启动时初始化定时任务
 */
@Slf4j
@Component
public class TaskInitializer implements ApplicationRunner {

    @Autowired
    private DownloadTaskService taskService;

    @Autowired
    private TaskSchedulerService schedulerService;

    @Override
    public void run(ApplicationArguments args) {
        log.info("开始初始化定时任务...");
        try {
            List<DownloadTask> tasks = taskService.getEnabledTasks();
            for (DownloadTask task : tasks) {
                schedulerService.scheduleTask(task);
            }
            log.info("定时任务初始化完成，共加载 {} 个启用任务", tasks.size());
        } catch (Exception e) {
            log.error("定时任务初始化失败", e);
        }
    }
}

